package com.example.administrator.basedemos.activity;

import android.view.View;
import android.widget.Button;

import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.api.GankApi;
import com.example.administrator.basedemos.base.BaseActivity;
import com.example.administrator.basedemos.bean.AndroidDataBean;
import com.example.administrator.basedemos.edu.MyBaseCallBack;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.TmtUtils;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * 创建者     ZCL
 * 创建时间   2018/3/20 15:12
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class RetrofitEduActivity extends BaseActivity {

    private static final String TAG ="RetrofitEduActivity";
    @BindView(R.id.button)
    Button mButton;
    private GankApi mEduApiService;

    @Override
    public void initView() {
        String baseEduUrl = "http://gank.io/";
        Retrofit.Builder builder = new Retrofit.Builder();
        Retrofit retrofit = builder.addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseEduUrl)
                .build();

        mEduApiService = retrofit.create(GankApi.class);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        super.initListener();
    }

     @OnClick({R.id.button,R.id.button_post,R.id.button_get_url})
         public void OnClik(View v){
             switch (v.getId()) {
                         case R.id.button:
                                mEduApiService.eduGet("Android",10,1).enqueue(new   MyBaseCallBack<AndroidDataBean>() {
                                    @Override
                                    protected void doSuccess(AndroidDataBean body) {
                             TmtUtils.midToast(RetrofitEduActivity.this,body.getResults().get(0).getDesc());
                                        LogUtil.util(TAG,"返回数据是:"+body.toString());
                                    }
                                });
                             break;
                         case R.id.button_post:
                                mEduApiService.eduPost("http://gank.io/api/data/Android/10/1","测试post",
                                        "zcl","Android","true").enqueue(new Callback<ResponseBody>() {

                                    @Override
                                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                                        ResponseBody s = response.body();
                                        if(response.isSuccessful()){
                                            try {
                                                LogUtil.util(TAG,"返回数据是:"+ s.string());
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }

                                        }else{
                                            LogUtil.util(TAG,"errorCode:"+response.code()+",message:"+response.message()
                                                    +",responseBody"+response.errorBody().toString());
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                                            LogUtil.util(TAG,"failed:"+t.getLocalizedMessage());

                                    }
                                });
                             break;

                 case R.id.button_get_url:
                     mEduApiService.eduGetUrl("api/data/Android/10/1").enqueue(new   MyBaseCallBack<AndroidDataBean>() {
                         @Override
                         protected void doSuccess(AndroidDataBean body) {
                             LogUtil.util(TAG,"返回数据是:"+body.toString());
                             TmtUtils.midToast(RetrofitEduActivity.this,body.getResults().get(0).getDesc());
                         }
                     });
                     break;
                         default:
                             break;
                     }
         }
    @Override
    public int setLayoutId() {
        return R.layout.activity_edu;
    }
}